package com.atguigu.spzx.cloud.gateway.filters;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.util.List;

/**
 * BelongProject: springCloud
 * BelongPackage: com.atguigu.spzx.cloud.gateway.filters
 * Description:
 *
 * @Author 段鑫涛
 * @Create 2025/5/26 20:55
 * @Version 1.0
 */
@Component
public class MyAuthFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        System.out.println("自定义过滤器");

        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        // 获得请求中的哪些信息
        RequestPath path = request.getPath();
        URI uri = request.getURI();
        HttpHeaders headers = request.getHeaders();
        List<String> list = headers.get("Truth");
        if(list!=null&&list.size()>0){
            String s = list.get(0);
            System.out.println(s);
        }

        //统一鉴权
        System.out.println("统一鉴权");
        //传递鉴权结果
        request.mutate().header("Truth","o is good");
        exchange.mutate().request(request);
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -1;
    }
}
